<?php
if (!defined('IN_IPB')) exit("<h1>Incorrect access</h1> You may not access this file without the acp.");

class transfer extends template_plugin
{
	var $ipsclass;
	
	var $name = "Transfer";
	var $multiple = 1;
	
	var $settings = array();
	
	function _load(&$plugin)
	{
		parent::_load($plugin);
		
		if($plugin['link'])
		{
			$this->settings = unserialize($plugin['plugin_config']);
		}
		else
		{
			$this->settings['send_money'] = 1;
			$this->settings['send_items'] = 1;
			$this->settings['fee_money'] = 5;
			$this->settings['fee_items'] = 5;
			$this->settings['item_fee_on'] = 0;
			$this->settings['allow_items'] = array_keys($this->ipsclass->cache['market_items']);
			$this->settings['send_pm'] = 1;
			$this->settings['subject'] = "Transferring";
			$this->settings['message'] = 
<<<EOF
{member_name_to},

I have sent you:[list]
[*]{currency_name} {money_amount}
[/list]{item_list}

Sincerly,
{member_name_from}

[b]This is an automated message[/b]
EOF;
			$this->settings['allow_anon'] = 0;
			$this->settings['anon_sub'] = "Anonymous Transferring";
			$this->settings['anon_msg'] = 
<<<EOF
{member_name_to},

I have sent you:[list]
[*]{currency_name} {money_amount}
[/list]{item_list}

Anonymous

[b]This is an automated message[/b]
EOF;
		}
	}
	
	function acp_settings()
	{
		$this->ipsclass->parser->parse_smilies   = 1;
		$this->ipsclass->parser->parse_html      = 1;
		$this->ipsclass->parser->parse_bbcode    = 1;
		
		if ( $this->ipsclass->han_editor->method == 'rte' )
		{
			$value = $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $this->settings['message'] ) );
			$value = $this->ipsclass->parser->convert_ipb_html_to_html( $value );
			$anon = $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $this->settings['anon_msg'] ) );
			$anon = $this->ipsclass->parser->convert_ipb_html_to_html( $anon );
		}
		else
		{
			$value = $this->ipsclass->parser->pre_edit_parse( $this->settings['message'] );
			$anon = $this->ipsclass->parser->pre_edit_parse( $this->settings['anon_msg'] );
		}
		
		$items = array();
		foreach($this->ipsclass->cache['market_items'] as $item_id => $item)
		{
			$items[] = array($item_id,$item['item_name']);
		}
		
		$html = "";
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Allow members to send money to other members</b>",
															$this->ipsclass->adskin->form_yes_no('send_money',$this->settings['send_money']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>If allowed, what is the percentage fee on the money sent</b>",
															$this->ipsclass->adskin->form_simple_input('fee_money',$this->settings['fee_money'])."%",
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Allow members to send items to other members</b>",
															$this->ipsclass->adskin->form_yes_no('send_items',$this->settings['send_items']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Allow members to send items to other members</b>",
															$this->ipsclass->adskin->form_multiselect('allow_items[]',$items,$this->settings['allow_items'],min(10,count($items))),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>If allowed, what is the percentage fee on the items sent</b>",
															$this->ipsclass->adskin->form_simple_input('fee_items',$this->settings['fee_items'])."%",
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Charge the fee based on what price?</b>",
															$this->ipsclass->adskin->form_dropdown('item_fee_on',array(array(0,'On the bought price the inventory item is bought on'),array(1,'On the resell price on the inventory item'),array(2,'On the buying price for the item being listed'),array(3,'On the resell price for the item being listed')),$this->settings['item_fee_on']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Anonymous transfer",
															$this->ipsclass->adskin->form_dropdown('allow_anon',array(array(0,'Do not allow anonymous transfer'),array(1,'Allow anonymous transfer as an option'),array(2,'All transfers are anonymous')),$this->settings['allow_anon']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Send a Message?",
															$this->ipsclass->adskin->form_dropdown('send_pm',array(array(0,'Do not send a message'),array(1,'Send a PM'),array(2,'Send an email')),$this->settings['send_pm']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>General Subject:</b>",
															$this->ipsclass->adskin->form_input('subject',$this->settings['subject']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>General Message:</b><br />Tags:<br />{member_name_from} = Member Name of Sender<br />{member_name_to} = Member Name of Reciever<br />{money_amount} = The amount of money being sent<br />{currency_name} = Currency Name<br />{item_list} = Items in a list of items<br />{item_names} = Items in a series",
															$this->ipsclass->han_editor->show_editor( $value, 'message' ),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Anonymous Transfer Subject:</b><br />Only allowed when sending a message is enabled.",
															$this->ipsclass->adskin->form_input('anon_sub',$this->settings['anon_sub']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Anonymous Transfer Message:</b><br />Only allowed when sending a message is enabled.<br />Tags:<br />{member_name_to} = Member Name of Reciever<br />{money_amount} = The amount of money being sent<br />{currency_name} = Currency Name<br />{item_list} = Items in a list of items<br />{item_names} = Items in a series",
															$this->ipsclass->han_editor->show_editor( $anon, 'anon_msg' ),
													)	);
		
		return $html;
	}
	
	function acp_save_settings(&$input)
	{
		$this->ipsclass->parser->parse_smilies   = 1;
		$this->ipsclass->parser->parse_html      = 1;
		$this->ipsclass->parser->parse_bbcode    = 1;
		
		$save = array();
		$save['send_money'] = $this->ipsclass->input['send_money'] & 1;
		$save['send_items'] = $this->ipsclass->input['send_items'] & 1;
		$save['allow_items'] = $this->ipsclass->input['allow_items'] ? $this->ipsclass->input['allow_items'] : array();
		$save['fee_money'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['fee_money']);
		$save['fee_items'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['fee_items']);
		$save['item_fee_on'] = min(max(intval($this->ipsclass->input['item_fee_on']),0),3);
		$save['send_pm'] = min(max(intval($this->ipsclass->input['send_pm']),0),2);
		$save['subject'] = $this->ipsclass->input['subject'];
        $save['message'] = $this->ipsclass->han_editor->process_raw_post( 'message' );
		$save['message'] = $this->ipsclass->parser->pre_edit_parse( $this->ipsclass->parser->pre_db_parse( $save['message'] ) );
		$save['subject'] = $this->ipsclass->input['subject'];
        $save['message'] = $this->ipsclass->han_editor->process_raw_post( 'message' );
		$save['message'] = $this->ipsclass->parser->pre_edit_parse( $this->ipsclass->parser->pre_db_parse( $save['message'] ) );
		$save['allow_anon'] = min(max(intval($this->ipsclass->input['allow_anon']),0),2);
		$save['anon_sub'] = $this->ipsclass->input['anon_sub'];
        $save['anon_msg'] = $this->ipsclass->han_editor->process_raw_post( 'anon_msg' );
		$save['anon_msg'] = $this->ipsclass->parser->pre_edit_parse( $this->ipsclass->parser->pre_db_parse( $save['anon_msg'] ) );
		
		$input['plugin_config'] = serialize($save);
	}
	
	function acp_install()
	{
		$this->ipsclass->DB->load_cache_file(ROOT_PATH.'sources/market/sql/'.$this->ipsclass->vars['sql_driver'].'_plugin_queries.php','sql_plugin_queries');
		
		if($this->ipsclass->market_class->count_plugin_filename($this->plugin['filename']) == 0)
		{
			$this->ipsclass->DB->build_query_from_cache('table_transfer_logs',array(),'sql_plugin_queries');
			$this->ipsclass->DB->exec_query();
			$this->ipsclass->DB->build_query_from_cache('table_transfer_item_logs',array(),'sql_plugin_queries');
			$this->ipsclass->DB->exec_query();
		}
	}
	
	function acp_uninstall()
	{
		if($this->ipsclass->market_class->count_plugin_filename($this->plugin['filename']) == 1)
		{
			$this->ipsclass->DB->sql_drop_table( 'market_transfer_logs' );
			$this->ipsclass->DB->sql_drop_table( 'market_transfer_item_logs' );
		}
	}
	
	function acp()
	{
		$LIMIT = 50;
		
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		
		$this->ipsclass->admin->page_title  = "Welcome to transfer logs page";
		$this->ipsclass->admin->page_detail = "Welcome to transfer logs page.";
		
		$total = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_transfer_logs','where'=>"l_link='".$this->plugin['link']."'"));
		$page = $this->ipsclass->adskin->build_pagelinks(	array(	'TOTAL_POSS'   => $total['total'],
																	'PER_PAGE'     => $LIMIT,
																	'USE_ST'       => 'st',
																	'CUR_ST_VAL'   => $this->ipsclass->input['st'],
																	'L_SINGLE'     => "Single Page",
																	'BASE_URL'     => $this->ipsclass->base_url."&amp;".$this->ipsclass->form_code
															)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_standalone_row($page,"left");
		
		$this->ipsclass->html .= "<br />";
		
		$this->ipsclass->adskin->td_header[] = array( "When"                , "" );
		$this->ipsclass->adskin->td_header[] = array( "From's IP"           , "" );
		$this->ipsclass->adskin->td_header[] = array( "From"                , "" );
		$this->ipsclass->adskin->td_header[] = array( "To"                  , "" );
		if($this->settings['send_money'])
		{
			$this->ipsclass->adskin->td_header[] = array( "Money Amount"    , "" );
		}
		if($this->settings['send_items'])
		{
			$this->ipsclass->adskin->td_header[] = array( "Number of Items" , "" );
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Transfer Logs");
		
		$this->ipsclass->DB->build_query(array(
													'select' => 'l.*',
													'from' => array('market_transfer_logs'=>'l'),
													'where' => "l_link='".$this->plugin['link']."'",
													'add_join' => array(
																			array(
																					'select' => 't.members_display_name as to_name, t.mgroup as to_group',
																					'from' => array('members'=>'t'),
																					'where' => "t.id=l.l_to",
																					'type' => 'left',
																				),
																			array(
																					'select' => 'f.members_display_name as from_name, f.mgroup as from_group',
																					'from' => array('members'=>'f'),
																					'where' => "f.id=l.l_from",
																					'type' => 'left',
																				),
																		),
													'order' => 'l_time DESC',
													'limit' => array($this->ipsclass->input['st'],$LIMIT)
										)		);
		$this->ipsclass->DB->exec_query();
		$logs = array();
		while($log = $this->ipsclass->DB->fetch_row())
		{
			$log['item_count'] = 0;
			$log['to_name'] = $this->ipsclass->market_admin->format_name($log['l_to'],$log['to_name'],$log['to_group']);
			$log['from_name'] = $this->ipsclass->market_admin->format_name($log['l_from'],$log['from_name'],$log['from_group']);
			$logs[$log['l_id']] = $log;
		}
		
		if($this->settings['send_items'])
		{
			if(count($logs))
			{
				$this->ipsclass->DB->build_query(array('select'=>'COUNT(*) as total,log_id','from'=>'market_transfer_item_logs','where'=>"log_id IN (".implode(", ", array_keys($logs)).")",'group'=>'log_id'));
				$this->ipsclass->DB->exec_query();
				while($item_log = $this->ipsclass->DB->fetch_row())
				{
					$logs[$item_log['log_id']]['item_count'] = $item_log['total'];
				}
			}
		}
		
		foreach($logs as $log)
		{
			$row = array( $this->ipsclass->get_date($log['l_time'],'SHORT'),
						  $log['l_ip_address'],
						  $log['from_name'],
						  $log['to_name'],
						);
			if($this->settings['send_money'])
			{
				$row[] = $this->ipsclass->numbers->format_currency($log['l_amount']);
			}
			if($this->settings['send_items'])
			{
				$row[] = $log['item_count'];
			}
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( $row );
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_standalone_row($page,"left");
		
		$this->ipsclass->admin->output();
	}
	
	function run()
	{
		$this->ipsclass->load_template('skin_market_plugin');
		$this->ipsclass->load_language('lang_market_plugin');
		
		$this->ipsclass->market_class->title[] = array($this->plugin['name'],'market_'.$this->plugin['link']);
		if(in_array($this->ipsclass->member['mgroup'],$this->plugin['use_permission']))
		{
			$this->ipsclass->market_class->menu[] = array(
															"<b><a href='{$this->ipsclass->market_class->base_url}plugin={$this->ipsclass->input['plugin']}'>".$this->ipsclass->lang['transfer_transfer']."</a></b>",
															"<b><a href='{$this->ipsclass->market_class->base_url}plugin={$this->ipsclass->input['plugin']}&amp;do=logs'>".$this->ipsclass->lang['transfer_logs']."</a></b>",
														);
			
			$this->settings['fee_money'] = $this->ipsclass->numbers->multiply($this->settings['fee_money'],$this->ipsclass->member['market_deduct']);
			$this->settings['fee_items'] = $this->ipsclass->numbers->multiply($this->settings['fee_items'],$this->ipsclass->member['market_deduct']);
			
			$do_redirect = array(
									'form' => 'form',
									'send' => 'send',
									'logs' => 'logs',
									'item_log' => 'item_log',
								);
			
			if(!isset($do_redirect[$this->ipsclass->input['do']]))
			{
				$this->ipsclass->input['do'] = 'form';
			}
			
			$func = 'run_'.$this->ipsclass->input['do'];
			$this->$func();
		}
		else
		{
			$this->ipsclass->market_class->menu[] = array(
															"<b><a href='{$this->ipsclass->market_class->base_url}plugin={$this->ipsclass->input['plugin']}'>".$this->ipsclass->lang['transfer']."</a></b>",
														);
			
			$this->ipsclass->run_form();
		}
	}
	
	function run_form()
	{
		$this->ipsclass->subtitle = $this->plugin['name'];
		$this->ipsclass->nav = array("<a href='{$this->ipsclass->market_class->base_url}plugin={$this->ipsclass->input['plugin']}'>".$this->plugin['name']."</a>");
		
		$items = array();
		foreach($this->ipsclass->market_class->inventory as $item_id => $item_group)
		{
			if(!in_array($item_id,$this->settings['allow_items']))
			{
				continue;
			}
			foreach($item_group as $inv_item)
			{
				$inv_item['time_bought'] = $this->ipsclass->get_date($inv_item['time_bought'],'TINY');
				$inv_item['fprice_bought'] = $this->ipsclass->numbers->compare($inv_item['price_bought'],0) <= 0 ? $this->ipsclass->lang['free'] : $this->ipsclass->numbers->format_currency($inv_item['price_bought']);
				$inv_item['fresell_price'] = $this->ipsclass->numbers->compare($inv_item['resell_price'],0) <= 0 ? $this->ipsclass->lang['trash'] : $this->ipsclass->numbers->format_currency($inv_item['resell_price']);
				$inv_item['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$inv_item['item']]->parse_single_description($inv_item)));
				$items[] = $inv_item;
			}
		}
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->transfer_form($this->settings,$items);
	}
	
	function run_send()
	{
		$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"members_l_display_name='".strtolower($this->ipsclass->input['member_name'])."'"));
		
		if(!$member || !intval($member['id']))
		{
			$this->ipsclass->Error(array('MSG'=>'no_such_user'));
		}
		
		if($member['id'] == $this->ipsclass->member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'transfer_cannot_send_self'));
		}
		
		$this->ipsclass->input['money_amount'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['money_amount'],'int');
		
		$fee = 0;
		
		if($this->settings['send_money'])
		{
			if($this->ipsclass->numbers->compare($this->ipsclass->input['money_amount'],0) < 0)
			{
				$this->ipsclass->Error(array('MSG'=>'transfer_notenoughmoney'));
			}
			$fee += $this->ipsclass->numbers->round($this->ipsclass->numbers->multiply($this->ipsclass->input['money_amount'],$this->settings['fee_money'],.01));
		}
		else
		{
			$this->ipsclass->input['money_amount'] = 0;
		}
		
		$inv_ids = array();
		if($this->settings['send_items'] && is_array($this->ipsclass->input['item']) && count($this->ipsclass->input['item']))
		{
			$inventory = array();
			$count = 0;
			$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_inventory','where'=>"member_id=".$member['id']));
			$this->ipsclass->DB->exec_query();
			while($item = $this->ipsclass->DB->fetch_row())
			{
				$inventory[$item['item']]++;
				$count++;
			}
			foreach($this->ipsclass->input['item'] as $key => $inv_id)
			{
				$inv_id = intval($inv_id);
				if($key == $inv_id && isset($this->ipsclass->market_class->inventory_id[$inv_id]) && in_array($this->ipsclass->market_class->inventory_id[$inv_id]['item'],$this->settings['allow_items']))
				{
					$inv_ids[] = $inv_id;
				}
				else
				{
					continue;
				}
				
				$inventory[$this->ipsclass->market_class->inventory_id[$inv_id]['item']]++;
				$count++;
				if($this->ipsclass->cache['market_items'][$this->ipsclass->market_class->inventory_id[$inv_id]['item']]['inventory_limit'] && $inventory[$this->ipsclass->market_class->inventory_id[$inv_id]['item']] > $this->ipsclass->cache['market_items'][$this->ipsclass->market_class->inventory_id[$inv_id]['item']]['inventory_limit'])
				{
					$this->ipsclass->Error(array('MSG'=>'transfer_too_many_items'));
				}
				if($this->ipsclass->vars['market_inventory_max'] && $count > $this->ipsclass->vars['market_inventory_max'])
				{
					$this->ipsclass->Error(array('MSG'=>'transfer_too_many_items'));
				}
				if($this->settings['item_fee_on'] == 0)
				{
					$value = $this->ipsclass->market_class->inventory_id[$inv_id]['price_bought'];
				}
				elseif($this->settings['item_fee_on'] == 1)
				{
					$value = $this->ipsclass->market_class->inventory_id[$inv_id]['resell_price'];
				}
				elseif($this->settings['item_fee_on'] == 2)
				{
					$value = $this->ipsclass->cache['market_items'][$this->ipsclass->market_class->inventory_id[$inv_id]['item']]['price'];
				}
				elseif($this->settings['item_fee_on'] == 3)
				{
					$value = $this->ipsclass->cache['market_items'][$this->ipsclass->market_class->inventory_id[$inv_id]['item']]['resell'];
				}
				else
				{
					$value = 0;
				}
				
				$fee += $this->ipsclass->numbers->round($this->ipsclass->numbers->multiply($value,$this->settings['fee_items'],.01));
			}
		}
		
		if($this->settings['send_money'] && $this->settings['send_items'])
		{
			if($this->ipsclass->numbers->compare($this->ipsclass->input['money_amount'],0) == 0 && count($inv_ids) == 0)
			{
				$this->ipsclass->Error(array('MSG'=>'transfer_error_nothing'));
			}
		}
		elseif($this->settings['send_money'])
		{
			if($this->ipsclass->numbers->compare($this->ipsclass->input['money_amount'],0) == 0)
			{
				$this->ipsclass->Error(array('MSG'=>'transfer_error_nothing'));
			}
		}
		elseif($this->settings['send_items'])
		{
			if(count($inv_ids) == 0)
			{
				$this->ipsclass->Error(array('MSG'=>'transfer_error_nothing'));
			}
		}
		if($this->ipsclass->numbers->compare($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$this->ipsclass->numbers->add($fee,$this->ipsclass->input['money_amount'])) < 0)
		{
			$this->ipsclass->Error(array('MSG'=>'transfer_notenoughmoney'));
		}
		
		if($this->settings['send_money'])
		{
			$this->ipsclass->DB->do_update('members',array(
																$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->subtract($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$this->ipsclass->numbers->add($fee,$this->ipsclass->input['money_amount'])),
					),"id=".$this->ipsclass->member['id']);
			$this->ipsclass->DB->do_update('members',array(
																$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->add($member[$this->ipsclass->vars['market_members_field']],$this->ipsclass->input['money_amount']),
									),"id=".$member['id']);
		}
		
		$this->ipsclass->market_class->record_fee($fee);
		
		$anon = 0;
		if($this->settings['allow_anon'] == 2 || ($this->settings['allow_anon'] == 1 && $this->ipsclass->input['anonymous']))
		{
			$anon = 1;
		}
		
		$this->ipsclass->DB->do_insert('market_transfer_logs',array(
																		'l_link' => $this->ipsclass->input['plugin'],
																		'l_time' => time(),
																		'l_ip_address' => $this->ipsclass->ip_address,
																		'l_from' => $this->ipsclass->member['id'],
																		'l_to' => $member['id'],
																		'l_amount' => $this->ipsclass->input['money_amount'],
																		'l_fee' => $fee,
																		'l_anon' => $anon,
										)							);
		$log_id = $this->ipsclass->DB->get_insert_id();
		
		$item_names = array();
		$item_list = "";
		if($this->settings['send_items'])
		{
			foreach($inv_ids as $inv_id)
			{
				$item_names[] = $this->ipsclass->cache['market_items'][$this->ipsclass->market_class->inventory_id[$inv_id]['item']]['item_name'];
				$item_list = "\n[*]".$this->ipsclass->cache['market_items'][$this->ipsclass->market_class->inventory_id[$inv_id]['item']]['item_name'];
				$this->ipsclass->DB->do_update('market_inventory',array('member_id'=>$member['id']),"i_id=".$inv_id);
				$this->ipsclass->DB->do_insert('market_transfer_item_logs',array(
																					'log_id' => $log_id,
																					'l_inventory_id' => $inv_id,
																					'l_item_id' => $this->ipsclass->market_class->inventory_id[$inv_id]['item'],
																					'l_price_bought' => $this->ipsclass->market_class->inventory_id[$inv_id]['price_bought'],
																					'l_resell_price' => $this->ipsclass->market_class->inventory_id[$inv_id]['resell_price'],
																					'l_item_category' => $this->ipsclass->cache['market_items'][$this->ipsclass->market_class->inventory_id[$inv_id]['item']]['category'],
																					'l_item_name' => $this->ipsclass->cache['market_items'][$this->ipsclass->market_class->inventory_id[$inv_id]['item']]['item_name'],
																					'l_item_description' => $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$this->ipsclass->market_class->inventory_id[$inv_id]['item']]->parse_single_description($this->ipsclass->market_class->inventory_id[$inv_id]))),
																					'l_item_icon' => $this->ipsclass->cache['market_items'][$this->ipsclass->market_class->inventory_id[$inv_id]['item']]['icon'],
																					'l_category_name' => $this->ipsclass->cache['market_category'][$this->ipsclass->cache['market_items'][$this->ipsclass->market_class->inventory_id[$inv_id]['item']]['category']]['cat_name'],
												)								);
			}
		}
		
		if($this->settings['send_pm'])
		{
			if($anon)
			{
				$subject = $this->settings['anon_sub'];
				$message = $this->settings['anon_msg'];
				
				$message = str_replace("{member_name_from}",$this->ipsclass->member['members_display_name'],$message);
			}
			else
			{
				$subject = $this->settings['subject'];
				$message = $this->settings['message'];
			}
			
			$message = str_replace("{member_name_to}",$member['members_display_name'],$message);
			$message = str_replace("{money_amount}",$this->ipsclass->numbers->format_currency($this->ipsclass->input['money_amount']),$message);
			$message = str_replace("{currency_name}",$this->ipsclass->vars['market_currency'],$message);
			$message = str_replace("{item_list}",$item_list,$message);
			$message = str_replace("{item_names}",implode(", ",$item_names),$message);
			
			if($this->settings['send_pm'] == 1)
			{
				require_once( ROOT_PATH.'sources/lib/func_msg.php' );
		 		
				$msg_lib           =  new func_msg();
				$msg_lib->ipsclass =& $this->ipsclass;
				$msg_lib->init();
				
				$msg_lib->to_by_id    = $member['id'];
				$msg_lib->from_member = $anon ? $member : $this->ipsclass->member;
				$msg_lib->msg_title   = $subject;
				$msg_lib->msg_post    = $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $message ) );
				$msg_lib->force_pm    = 1;
				
				$msg_lib->send_pm();
			}
			else
			{
				require_once( ROOT_PATH."sources/classes/class_email.php" );
				
				$email           =  new emailer();
				$email->ipsclass =& $this->ipsclass;
				$email->email_init();
				
				$email->subject = $subject;
				$email->message = $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $message ) );
				$email->to      = $member['email'];
		
				$email->send_mail();
			}
		}
		
		$this->ipsclass->market_class->redirect('transfer_sent',"plugin=".$this->ipsclass->input['plugin']);
	}
	
	function run_logs()
	{
		$this->ipsclass->nav = array("<a href='{$this->ipsclass->market_class->base_url}plugin={$this->ipsclass->input['plugin']}&amp;do=logs'>".$this->ipsclass->lang['transfer_logs']."</a>");
		
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		$LIMIT = 25;
		
		$this->ipsclass->lang['transfer_money_amount'] = str_replace("<#CURRENCY#>",$this->ipsclass->vars['market_currency'],$this->ipsclass->lang['transfer_money_amount']);
		
		$rows = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_transfer_logs','where'=>"l_link='".$this->ipsclass->input['plugin']."' AND (l_from=".$this->ipsclass->member['id']." OR l_to=".$this->ipsclass->member['id'].")"));
		$page_links = $this->ipsclass->build_pagelinks(	array(	'TOTAL_POSS'   => $rows['total'],
																'PER_PAGE'     => $LIMIT,
																'CUR_ST_VAL'   => $this->ipsclass->input['st'],
																'L_SINGLE'     => $this->ipsclass->compiled_templates['skin_global']->pagination_compile($this->ipsclass->compiled_templates['skin_global']->pagination_make_jump(),"","",$this->ipsclass->compiled_templates['skin_global']->pagination_current_page(1),"","",1,$LIMIT,$this->ipsclass->market_class->base_url."plugin={$this->ipsclass->input['plugin']}&amp;do=logs",0,'st'),
																'BASE_URL'     => $this->ipsclass->market_class->base_url."plugin={$this->ipsclass->input['plugin']}&amp;do=logs",
																'USE_ST'       => 'st'
														)	);
		
		$this->ipsclass->DB->build_query(array(
												'select' => 'l.*',
												'from' => array('market_transfer_logs'=>'l'),
												'where' => "l.l_link='".$this->ipsclass->input['plugin']."' AND (l.l_from=".$this->ipsclass->member['id']." OR l.l_to=".$this->ipsclass->member['id'].")",
												'add_join' => array(
																		array(
																				'select' => 't.members_display_name as to_name, t.mgroup as to_group',
																				'from' => array('members'=>'t'),
																				'where' => "l.l_to=t.id",
																				'type' => 'left',
																			),
																		array(
																				'select' => 'f.members_display_name as from_name, f.mgroup as from_group',
																				'from' => array('members'=>'f'),
																				'where' => "l.l_from=f.id",
																				'type' => 'left',
																			),
																	),
												'order' => "l_time DESC",
												'limit' => array($this->ipsclass->input['st'],$LIMIT),
										)	);
		$this->ipsclass->DB->exec_query();
		$logs = array();
		while($log = $this->ipsclass->DB->fetch_row())
		{
			$log['item_count'] = 0;
			$log['l_amount'] = $this->ipsclass->numbers->format_currency($log['l_amount']);
			$log['l_time'] = $this->ipsclass->get_date($log['l_time'],'SHORT');
			$log['to_name'] = $this->ipsclass->market_class->format_name($log['l_to'],$log['to_name'],$log['to_group']);
			$log['from_name'] = $log['l_to'] == $this->ipsclass->member['id'] && $log['l_anon'] ? $this->ipsclass->lang['transfer_anonymous'] : $this->ipsclass->market_class->format_name($log['l_from'],$log['from_name'],$log['from_group']);
			$logs[$log['l_id']] = $log;
		}
		
		if($this->settings['send_items'] && count($logs))
		{
			$this->ipsclass->DB->build_query(array('select'=>'COUNT(*) as total,log_id','from'=>'market_transfer_item_logs','where'=>"log_id IN (".implode(", ", array_keys($logs)).")",'group'=>'log_id'));
			$this->ipsclass->DB->exec_query();
			while($item_log = $this->ipsclass->DB->fetch_row())
			{
				$logs[$item_log['log_id']]['item_count'] = $item_log['total'];
			}
		}
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->transfer_log_page($this->settings,$logs,$page_links);
	}
	
	function run_item_log()
	{
		if(!$this->settings['send_items'])
		{
			$this->ipsclass->Error(array('MSG'=>'transfer_invalid_log'));
		}
		
		$this->ipsclass->input['log'] = intval($this->ipsclass->input['log']);
		$log = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_transfer_logs','where'=>"l_id=".$this->ipsclass->input['log']));
		
		if(!$log || ($log['l_to'] != $this->ipsclass->member['id'] && $log['l_from'] != $this->ipsclass->member['id']) || $log['l_link'] != $this->ipsclass->input['plugin'])
		{
			$this->ipsclass->Error(array('MSG'=>'transfer_invalid_log'));
		}
		
		$log['l_amount'] = $this->ipsclass->do_number_format($log['l_amount']);
		$log['l_time'] = $this->ipsclass->get_date($log['l_time'],'SHORT');
		
		$this->ipsclass->lang['transfer_money_amount'] = str_replace("<#CURRENCY#>",$this->ipsclass->vars['market_currency'],$this->ipsclass->lang['transfer_money_amount']);
		
		$item_html = array();
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_transfer_item_logs','where'=>"log_id=".$this->ipsclass->input['log']));
		$this->ipsclass->DB->exec_query();
		$log['item_count'] = $this->ipsclass->DB->get_num_rows();
		if($log['item_count'] == 0)
		{
			$this->ipsclass->Error(array('MSG'=>'transfer_no_items'));
		}
		while($item_log = $this->ipsclass->DB->fetch_row())
		{
			$item_log['l_price_bought'] = $this->ipsclass->numbers->compare($item_log['l_price_bought'],0) <= 0 ? $this->ipsclass->lang['free'] : $this->ipsclass->numbers->format_currency($item_log['l_price_bought']);
			$item_log['l_resell_price'] = $this->ipsclass->numbers->compare($item_log['l_resell_price'],0) <= 0 ? $this->ipsclass->lang['trash'] : $this->ipsclass->numbers->format_currency($item_log['l_resell_price']);
			$item_html[] = $item_log;
		}
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->transfer_item_log_page($this->settings,$log,$item_html);
	}
}
?>